﻿<!DOCTYPE html>
<html>
<head>
<!--
  Copyright (c) 2016-2018 Jean-Marc VIGLINO, 
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
  <title>ol-ext: Splitter interaction</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <meta name="description" content="an OL3 interaction to draw regular polygon" />
  <meta name="keywords" content="ol3, interaction, draw, regular, rectangle, circle, triangle" />

  <link rel="stylesheet" href="../style.css" />

  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css" />
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>
  <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL,Object.assign"></script>
  
  <!-- ol-ext -->
  <link rel="stylesheet" href="../../dist/ol-ext.css" />
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>
  <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
  <script src="https://unpkg.com/elm-pep"></script>

</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <a href="../../index.html">
    <h1>ol-ext: Splitter interaction</h1>
  </a>
  <div class="info">
    <b>ol.interaction.Splitter</b> is an interaction that acts as a split feature agent while editing vector features (LineString).
    <br/>
    The features are cloned. The interaction fires a <i>beforesplit</i> and <i>aftersplit</i> event on the source.
    You can listen to the event on the interaction itself or on the source of the splitted feature.
    <br/>
    The sources provided to the interaction must have a spatial index to be splitable (configured with useSpatialIndex set to true).
    <br />
    <br />
    <b>TODO:</b> Split with more than one feature (when moving nodes)
    <br />
    <b>NB:</b> If you look to an interaction that split a linestring just on clicking a point on the feature, look at <a href="map.interaction.split.html">ol.interaction.Split</a>.
  </div>

  <!-- Map div -->
  <div id="map" style="width:600px; height:400px;"></div>

  <div class="options" >
    <h2>Options:</h2>
    <ul><li>
      Operation: 
      <input type="radio" name="op" value="draw" checked="checked" /><label> Draw</label>
      <input type="radio" name="op" value="modify"  /><label> Modify</label>
    </li></ul>
    <div style="background:white; padding:0 0.45em;"><span id="info"></span>&nbsp;</div>
  </div>
  

  <script type="text/javascript">
    // Layers
    var layers = [new ol.layer.Geoportail({ layer: 'ORTHOIMAGERY.ORTHOPHOTOS' })];
    // The map
    var map = new ol.Map({
      target: 'map',
      view: new ol.View({
        zoom: 5,
        center: [261720, 5951081]
      }),
      layers: layers
    });

    // New vector layer
    var vector = new ol.layer.Vector({
      name: 'Vecteur',
      source: new ol.source.Vector({ features: new ol.Collection() }),
      style: function(f) {
        return [
          new ol.style.Style({
            stroke: new ol.style.Stroke({ color: '#ffcc33',width: 2 }),
          }),
          new ol.style.Style({
            image: new ol.style.RegularShape({ radius: 4, points:4, fill: new ol.style.Fill({ color: '#f00' }) }),
            geometry: new ol.geom.MultiPoint([f.getGeometry().getFirstCoordinate(),f.getGeometry().getLastCoordinate()])
          })
        ]
      }
    })
    map.addLayer(vector);
    vector.getSource().addFeature(new ol.Feature(new ol.geom.LineString([[-288626, 5757848], [210354, 5576845], [34243, 6305749]])));
    vector.getSource().addFeature(new ol.Feature(new ol.geom.LineString([[406033, 5664901], [689767, 5718712], [699551, 6149206], [425601, 6183449]])));
    vector.getSource().addFeature(new ol.Feature(new ol.geom.Point([261720, 5951081])));

    // Add splitter before other interaction
    var splitter = new ol.interaction.Splitter({ source: vector.getSource() });
    map.addInteraction(splitter);

    // Add interactions to modify features
    var interactions = {
      select: new ol.interaction.Select (),
      modify: new ol.interaction.Modify ({ features: vector.getSource().getFeaturesCollection() }),
      draw: new ol.interaction.Draw({
        source: vector.getSource(),
        type: "LineString"
        // type: "Point"
      })
    }
    for (var i in interactions) map.addInteraction(interactions[i]);

    // Add a snap
    var snap = new ol.interaction.Snap({ source: vector.getSource() });
    map.addInteraction(snap);

    // Activate modification
    setInteraction = function () {
      //interactions.select.getFeatures().clear();
      var name = $('[name="op"]:checked').val();
      for (var i in interactions) {
        interactions[i].set("active", (i==name));
      }
    }
    setInteraction();

    // Radio control
    $("label").click(function(){$(this).prev().click();});
    $("input:radio").on("change",setInteraction);

    // Get information when splitting
    vector.getSource().on("aftersplit", function(e) {
      splitting = false;
      // console.log(e)
      $(".options > div").text(vector.getSource().getFeatures().length+" features"
          + " ("+e.featureAdded.length+" added, "+e.featureRemoved.length+" removed)");
    });
    
  </script>

</body>
</html>
